package com.dahuyou.io.model.aio.client;

import com.dahuyou.io.model.aio.ChannelAdapter;
import com.dahuyou.io.model.aio.ChannelHandler;

import java.io.IOException;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.charset.Charset;

public class AioClientHandler extends ChannelAdapter {

    public AioClientHandler(AsynchronousSocketChannel channel, Charset charset) {
        super(channel, charset);
    }

    @Override
    public void channelActive(ChannelHandler ctx) {
        try {
            System.out.println("链接报告信息:" + ctx.channel().getRemoteAddress());
            //通知客户端链接建立成功
//            ctx.writeAndFlush("hello nio server!!!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

   /* @Override
    public void completed(Integer result, ByteBuffer buffer) {
        buffer.flip();
        byte[] bytes = new byte[buffer.remaining()];
        buffer.get(bytes);
        String body;
        try {
            body = new String(bytes,"UTF-8");
            System.out.println("客户端收到结果22222:"+ body);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }*/

    @Override
    public void channelInactive(ChannelHandler ctx) {
    }

    @Override
    public void channelRead(ChannelHandler ctx, Object msg) {
        System.out.println("nio client receive: " + msg + "\r\n");
        ctx.writeAndFlush("nio client send back msg!\r\n");
//        ctx.writeAndFlush("nio client send back msg2!\r\n");
//        ctx.writeAndFlush("nio client send back msg3!\r\n");
    }

}
